#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "modbushandler.h"
#include <QFile>
#include <QTextStream>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();




private slots:
    void on_connectButton_clicked();
    void updateData(double voltage, double current, int soc, int soh,
                    double temperatures[6], double cellVoltages[16],
                    quint16 protectionFlags1, quint16 protectionFlags2,
                    double ratedCapacity,int cellCount,int cycleCount);

    void on_pushButton_ScanningCOM_clicked();
    void onBaudRateSelected(int index);
    void onAutoReadButtonClicked(); // 自动读取按钮的槽函数
    void updateEnergyDisplay(double chargeAh, double dischargeAh);
    void on_pushButton_clear_clicked();
    void on_recordButton_clicked();//数据导出
    void recordData(const QString &timestamp,double voltage, double current, int soc, int soh,
                                double temperatures[6], double cellVoltages[16],
                                quint16 protectionFlags1, quint16 protectionFlags2,
                                double ratedCapacity,int cellCount,int cycleCount);
    void updateIndicator();

private:
    Ui::MainWindow *ui;
    ModbusHandler *modbusHandler;
    double chargeAh_save;
    double dischargeAh_save;
    QTimer *refreshTimer;
    QTimer *indicatorTimer; // 用于控制指示灯闪烁的定时器
    bool autoReadActive;
    bool portOpen = false;
    bool isRecording = false;     // 数据记录状态
    bool isDataReceived = false;  // 标记是否有数据接收
    QFile recordFile;             // 数据文件
    QTextStream recordStream;     // 文件流
};

#endif // MAINWINDOW_H
